﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Bolseye;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {

        int _m_refreshInterval = 5000;

        public Form2()
        {
            InitializeComponent();

            RefreshData();

            Timer t = new Timer();
            t.Interval = _m_refreshInterval;
            t.Tick += new EventHandler(t_Tick);
            t.Start();
        }

        void t_Tick(object sender, EventArgs e)
        {
            RefreshData();
        }


        DateTime lastRefresh = DateTime.MinValue;
        private void RefreshData()
        {
            if (DateTime.UtcNow.Subtract(lastRefresh).TotalMilliseconds < _m_refreshInterval)
                return;

            lastRefresh = DateTime.MaxValue;

            var db = Support.GetDB();

            var compra = (
                            from o in db.Operaciones
                            orderby o.ID descending
                            select o
                         ).FirstOrDefault();


            var resumen = Support.ObtenerResumenOperacion(compra.ID);

            lblSimbolo.Text = resumen.SimboloObj.Simb.ToUpper();

            lblCompra.Text = Support.ValueToString(resumen.PrecioCompra);
            lblCapital.Text = Support.ValueToString(resumen.OperacionObj.Capital);
            lblCosto.Text = Support.ValueToString(resumen.OperacionObj.CostoIngreso);
            lblCantidad.Text = resumen.OperacionObj.Cantidad.ToString();

            lblActual.Text = Support.ValueToString(resumen.PrecioActual);
            lblCapitalNew.Text = Support.ValueToString(resumen.CapitalActual);
            lblGciaBruta.Text = Support.ValueToString(resumen.GananciaBruta);

            lblCostoEgreso.Text = Support.ValueToString(resumen.CostoSalida);
            lblGciaNeta.Text = Support.ValueToString(resumen.GananciaNeta);

            lblVarCompra.Text = Support.ValueToPercent(resumen.VariacionRel);

            lblTasaMensual.Text = Support.ValueToPercent(resumen.TasaMensual);

            lastRefresh = DateTime.UtcNow;

            lblActualizacion.Text = DateTime.Now.ToString("HH:mm.ss, dddd dd, MMMM, yyyy");


            // HIGH, LOW, OPEN CLOSE VALUES RESPECTIVELY.
            // CLOSE VALUES ARE THE CURRENT VALUES.

            //chart1.Series[0].Points.AddXY(DateTime.UtcNow.AddMinutes(-5), 30, 10, 5, 20);
            //chart1.Series[0].Points.AddXY(DateTime.UtcNow.AddMinutes(-4), 30, 10, 5, 3);
            //chart1.Series[0].Points.AddXY(DateTime.UtcNow.AddMinutes(-2), 30, 10, 5, 25);
            
        }


        private void btnCerrar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
//